package com.yt.order.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import com.yt.order.models.service.IEmpRatingStatsService;
import com.yt.order.models.bo.EmpRatingStatsBO;
import com.yt.order.models.dto.EmpRatingStatsDTO;

import java.util.List;

/**
 * 员工星级表控制器
 *
 * @author zhou
 * @Date 2025-09-15 16:19:26
 */

@RestController
@RequestMapping("/api/empRatingStats")
@Slf4j
@Api(value = "EmpRatingStatsApiController",tags = "员工星级表Api接口")
public class EmpRatingStatsApiController {

    @DubboReference
    private IEmpRatingStatsService empRatingStatsService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<EmpRatingStatsDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        EmpRatingStatsDTO dto = empRatingStatsService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "员工星级表列表",notes = "员工星级表列表")
    @PostMapping("/queryPageList")
    public Result<Page<EmpRatingStatsDTO>> queryPageList(EmpRatingStatsBO bo){
        Wrapper<EmpRatingStatsDTO> wrapper = new EntityWrapper<>();
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<EmpRatingStatsDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = empRatingStatsService.selectPage(page, wrapper);

        return new Result<>(page);
    }
    @Scheduled(cron = "0 */5 * * * ?")
    @PostMapping("/starEmployee")
    @ApiOperation(value = "员工星级表",notes = "员工星级表")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result getStar(){
        //查询所有员工id
        List<Long> empIdList=empRatingStatsService.queryEmp();
        if (empIdList.isEmpty()){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        for (Long empId : empIdList){
            empRatingStatsService.updateStar(empId);
        }
        return new Result<>();
    }
}
